var longestCommonPrefix = function (strs) {
  if (strs === null || strs.length === 0) {
    return "";
  }
  let length = strs[0].length;
  let count = strs.length;
  for (let i = 0; i < length; i++) {
    let c = strs[0].charAt(i);
    for (let j = 1; j < count; j++) {
      if (i == strs[j].length || strs[j].charAt(i) != c) {
        return strs[0].substring(0, i);
      }
    }
  }
  return strs[0];
}

var longestCommonPrefix = function (strs) {
  if (strs.length == 0)
    return "";
  let ans = strs[0];
  for (let i = 1; i < strs.length; i++) {
    let j = 0;
    for (; j < ans.length && j < strs[i].length; j++) {
      if (ans[j] != strs[i][j])
        break;
    }
    ans = ans.substr(0, j);
    if (ans === "")
      return ans;
  }
  return ans;
}

// 比较简单的一款
var longestCommonPrefix = function (strs) {
  if (strs.length === 0) return ''
  if (strs.length === 1) return strs[0]
  let res = strs[0]
  for (let i = 1; i < strs.length; i++) {
    while (!strs[i].startsWith(res)) {
      res = res.slice(0, res.length - 1)
    }
  }
  return res
};

console.log(longestCommonPrefix(["flower", "flow", "flight"]));
